import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:ls_work/a_elder/models/waiting_right_entity.dart';
import 'package:ls_work/a_yyt/models/elder_time_line_entity.dart';
import 'package:ls_work/a_yyt/models/health_check_entity.dart';
import 'package:ls_work/a_yyt/page/medical_photo_list_page.dart';
import 'package:ls_work/a_yyt/widget/client_time_cell.dart';
import 'package:ls_work/mvp/svp_progress.dart';
import 'package:ls_work/net/HttpUtil.dart';
import 'package:ls_work/net/net.dart';
import 'package:ls_work/net/post_request.dart';
import 'package:ls_work/res/gaps.dart';
import 'package:ls_work/res/styles.dart';
import 'package:ls_work/util/app_navigator.dart';
import 'package:ls_work/util/toast.dart';
import 'package:ls_work/widgets/click_item.dart';

///评估审核
class PendingBizType1082 extends StatefulWidget {
  final WaitingRighInfo? info;
  final VoidCallback? callBack;
  PendingBizType1082({Key? key, this.info, this.callBack});
  @override
  _PendingBizType1082State createState() => _PendingBizType1082State();
}

class _PendingBizType1082State extends State<PendingBizType1082> {
  List<TimeLinesInfo> timeLineList = [];
  HealthCheckEntity _detailModel = HealthCheckEntity(listDetail: []);

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      fectElderTimeLine();
      _getDetailExamination();
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('体检报告审核'),
        elevation: 0.5,
      ),
      body: SingleChildScrollView(
        padding: EdgeInsets.fromLTRB(15, 30, 15, 80),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text.rich(TextSpan(children: [
              TextSpan(
                text: '   长者：',
                style: TextStyles.textGray18,
              ),
              TextSpan(
                text: '${widget.info?.elderName ?? ''}',
                style: TextStyles.textDark18,
              ),
            ])),
            Gaps.vGap16,
            Gaps.vGap10,
            Text.rich(TextSpan(children: [
              TextSpan(
                text: '   类型：',
                style: TextStyles.textGray18,
              ),
              TextSpan(
                text: '${widget.info?.bizParams?.serviceItem ?? ''}',
                style: TextStyles.textDark18,
              ),
            ])),
            Gaps.vGap16,
            Gaps.vGap10,
            Text.rich(TextSpan(children: [
              TextSpan(
                text: '   销售归属：',
                style: TextStyles.textGray18,
              ),
              TextSpan(
                text: '${_detailModel.ownerName ?? '无'}',
                style: TextStyles.textDark18,
              ),
            ])),
            Gaps.vGap16,
            Gaps.vGap16,
            Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: (_detailModel.listDetail??[]).map((e) {
                return ClickItem(
                  title: '${e.itemName ?? ''}',
                  titleStyle: TextStyles.textGray18,
                  contentStyle: TextStyles.textDark18,
                  content: '${e.examinationStatusName ?? ''}',
                  onTap: () {
                    AppNavigator.push(
                        context,
                        MedicalPhotoListPage(
                          itemInfo: e,
                          customerId: _detailModel.customerId,
                          examinationRecordId: _detailModel.examinationRecordId,
                          callBack: _getDetailExamination,
                        ));
                  },
                );
              }).toList(),
            ),
            Gaps.vGap50,
            Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: timeLineList.map((e) {
                int index = timeLineList.indexOf(e);
                return ClientTimeCell(
                  count: timeLineList.length,
                  info: timeLineList[index],
                );
              }).toList(),
            ),
          ],
        ),
      ),
    );
  }

  void fectElderTimeLine() {
    var map = {
      'traceType': 60,
      'relationId': widget.info?.bizId,
      'customerId': widget.info?.customerId,
    };

    SVProgressHUD page = SVProgressHUD(context);
    page.showProgress();
    HttpUtil.instance
        .getDio()
        .post(HttpApi.elderTimeLine, data:map,options: PostRequest.urlencodedHeader)
        .then((r) {
      ///
      page.closeProgress();
      if (r.statusCode == 200) {
        Map<String, dynamic> map = json.decode(r.toString());
        if (map["code"].toString() == "0") {
          if (map['data'] != null) {
            TimeLineEntity model = TimeLineEntity.fromJson(map['data']);
            // print('🍎 - length - ${model.rows.length}');
            if ((model.rows??[]).length >= 0) {
              setState(() {
                timeLineList = model.rows??[];
              });
            }
          } else {
            Toast.show(map["msg"]);
          }
        } else {
          Toast.show(map["msg"]);
        }
      } else {
        Toast.show(r.statusCode.toString());
      }
    }).catchError((e) {
      page.closeProgress();
      print(e.toString());
      Toast.show(getErrorMessage(e));
    });
  }

  ///体检项目列表
  void _getDetailExamination() {
    var map = {
      'customerId': widget.info?.customerId,
      'examinationRecordId': widget.info?.bizParams?.examinationRecordId,
    };
    SVProgressHUD page = SVProgressHUD(context);
    page.showProgress();
    HttpUtil.instance
        .getDio()
        .post(HttpApi.listReviewDetail,
            data: {'jsonStr': jsonEncode(map)},options: PostRequest.urlencodedHeader)
        .then((r) {
      ///
      page.closeProgress();
      if (r.statusCode == 200) {
        Map<String, dynamic> map = json.decode(r.toString());
        if (map["code"].toString() == "0") {
          if (map['data'] != null) {
            HealthCheckEntity model = HealthCheckEntity.fromJson(map['data']);
            setState(() {
              _detailModel = model;
            });
          } else {
            Toast.show(map["msg"]);
          }
        } else {
          Toast.show(map["msg"]);
        }
      } else {
        Toast.show(r.statusCode.toString());
      }
    }).catchError((e) {
      page.closeProgress();
      print(e.toString());
      Toast.show(getErrorMessage(e));
    });
  }
}
